//
//  HomeViewController.swift
//  微博
//
//  Created by xm on 2018/3/17.
//  Copyright © 2018年 xm. All rights reserved.
//

import UIKit

//定义cell重用标示
private let homeOriginCellID="homeOriginCellID"//原创微博cell
private let homeRetweetedCellID="homeRetweetedCellID"//转发微博cell

class HomeViewController: BaseViewController {
    
    lazy var listViewModel=HomeStatusListViewModel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor=UIColor.red         
        
    }
    
    
    override func initView() {
        super.initView()
        
         navItem.leftBarButtonItem=UIBarButtonItem.init(title: "好友", target: nil, action:nil)
         let nib = UINib(nibName:"HomeOriginStatusViewCell", bundle: nil)
         tableView?.register(nib, forCellReuseIdentifier: homeOriginCellID) 
         tableView?.register(UINib(nibName:"HomeRetweetedStatusViewCell", bundle: nil),
                             forCellReuseIdentifier: homeRetweetedCellID)
         //设置行高
         tableView?.rowHeight=UITableViewAutomaticDimension//自适应行高
         tableView?.estimatedRowHeight=100//预估行高
        
         //取消分割线
         tableView?.separatorStyle = .none
        
         setupNavTitle()
    }
    
    override func loadData() {
        listViewModel.loadData(isPullUp: isPullUp){ (isSuccess) in
            self.refreshContol?.endRefreshing()//结束刷新
            self.isPullUp=false
            self.tableView?.reloadData()
        }
    }
    
    //设置标题按钮
    private func setupNavTitle() {
        let btn = TitleBtn(title: NetworkManager.shared.userAccount.screen_name)
        self.navItem.titleView=btn
        btn.addTarget(self, action: #selector(ontitleBtnClick), for: .touchUpInside)
    }
    
    @objc func ontitleBtnClick(btn: UIButton){
        //设置选中状态
        btn.isSelected = !btn.isSelected
        
    }
}


// MARK: - 表格数据源、代理方法
extension HomeViewController{
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listViewModel.dataList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let homeViewModel:HomeStatusViewModel=listViewModel.dataList[indexPath.row]
        var cell:HomeTableViewCell?
        if homeViewModel.status?.retweeted_status != nil{//转发微博
            cell = tableView.dequeueReusableCell(withIdentifier: homeRetweetedCellID, for: indexPath) as? HomeTableViewCell
        }else{
            cell = tableView.dequeueReusableCell(withIdentifier: homeOriginCellID, for: indexPath) as? HomeTableViewCell
        }
        cell?.statusViewModel=homeViewModel
        return cell!;
    }
    
}

 
